#pragma once
#include<queue> //根据原有队列改编一下
#include<thread>
#include<mutex> //pthread_mutex_t
#include<condition_variable> //pthread_condition_t

//异步写日志的日志队列
template<typename T>
class LockQueue{
public:
    //  多个工作线程写日志queue
    void Push(const T& data);
    // 一个线程读queue,写日志文件
    T Pop();
private:
    std::queue<T> m_queue;
    std::mutex m_mutex;
    std::condition_variable m_condvariable;

};

//模板无法分文件编写
template<typename T>
void LockQueue<T>::Push(const T&data){
    std::lock_guard<std::mutex> lock(m_mutex); //自动加锁
    m_queue.push(data);
    m_condvariable.notify_one();
    

}

template<typename T>
T LockQueue<T>::Pop(){
    std::unique_lock<std::mutex> lock(m_mutex);
    while(m_queue.empty()){
        //日志队列为空，线程进入wait状态,并把锁释放掉,唤醒后，重写加锁
        m_condvariable.wait(lock);
    }

    T data = m_queue.front();
    m_queue.pop();
    return data;
}

